Repository navigation
Conversation
| public static function first(string $charset, $item) | ||
| { | ||
| if (\is_array($item)) { | ||
| return [] === $item ? false : $item[array_key_first($item)]; |
There was a problem hiding this comment.
You may use array_first and array_last (PHP 8.5+)
There was a problem hiding this comment.
Could we add https://github.com/symfony/polyfill-php85? I see that we already requires polyfills for mbstring and ctype, so I would say yes.
Updating the PR and benchmarks ⏳
There was a problem hiding this comment.
Hum, shouldn't we check for \PHP_VERSION_ID >= 80500 before calling array_first() and array_last, instead of using the polyfill? I updated the PR's description and the polyfill is slower than the previous $item[array_key_first($item)] 🤔
There was a problem hiding this comment.
Check polyfill dev-main for this
There was a problem hiding this comment.
Tested with symfony/polyfill-php85 at 94c794cb5b (1.x head, with symfony/polyfill#726), on PHP 8.4:
first |
last |
|
|---|---|---|
3.x |
170-173 ns | 169-171 ns |
| polyfill | 123-126 ns | 139-142 ns |
| this PR | 82-83 ns | 82-85 ns |
What remains is the two userland calls (bootstrap.php, then Php85::array_first()), I would kept the \PHP_VERSION_ID check in Twig
dd3c755 to
d7464ba
Compare
d7464ba to
9d1aca9
Compare
| if (\is_array($item)) { | ||
| if ([] === $item) { | ||
| return false; | ||
| } |
There was a problem hiding this comment.
Less nested if and an optimisation for the empty string.
| if (\is_array($item)) { | |
| if ([] === $item) { | |
| return false; | |
| } | |
| if ([] === $item || '' === $item) { | |
| return false; | |
| } | |
| if (\is_array($item)) { |
There was a problem hiding this comment.
''|first outputs '' and not false in 3.x, this would be a BC break right?
However I'm moving the [] === $item check outside, thx
There was a problem hiding this comment.
Btw @fabpot, I tried to reproduce {{ ''|first }} on the Twig Playground, but I got the following error:
⚠️ An exception has been thrown during the rendering of a template ("Call to undefined function Twig\Extension\mb_substr()") in "index.twig" at line 1.
9d1aca9 to
0ace475
Compare
There was a problem hiding this comment.
This single change would do the same optimisation without duplicating some logic.
| if (!\is_array($value)) { | |
| $value = self::toArray($value, false); | |
| } |
| } | ||
|
|
||
| // two strings always end up on the "<=>" fallback of compare(), where "0 === <=>" is "==" | ||
| $valueIsString = \is_string($value); |
There was a problem hiding this comment.
\is_string($value) is already optimized by PHP, I'm not sure this is worth keeping the result in a variable.
`in` called `CoreExtension::compare()` for each element even when both sides are strings. `first` and `last` went through `slice()`, and `join` copied the array with `array_values()` before `implode()`. Two strings are now compared with `==`, which is what `compare()` ends up doing for them. `first` and `last` call `array_first()` and `array_last()` on an array on PHP 8.5, and read the first or last key directly before that (the `\PHP_VERSION_ID` check is resolved at compile time), and `join` no longer copies an array before joining it, with or without an `and` separator. `in` on 8 strings goes from 656-690 -> 243-317 ns, `first` from 167-169 -> 78-79 ns on PHP 8.4 and from 121-122 -> 53-54 ns on PHP 8.5, and `join` of 20 items from 316-319 -> 236-262 ns (433-440 -> 364-367 ns with an `and` separator). A 300-row page using the four goes from 0.864-0.894 -> 0.749-0.785 ms. New fixture cases (numeric strings, empty arrays) pass on 3.x too.
0ace475 to
8f20336
Compare
incalledCoreExtension::compare()for each element even when both sides are strings.firstandlastwent throughslice(), andjoincopied the array witharray_values()beforeimplode().Two strings are now compared with
==, which is whatcompare()ends up doing for them.firstandlastcallarray_first()andarray_last()on an array on PHP 8.5, and read the first or last key directly before that (the\PHP_VERSION_IDcheck is resolved at compile time), andjoinno longer copies an array before joining it, with or without anandseparator.inon 8 strings goes from 656-690 -> 243-317 ns,firstfrom 167-169 -> 78-79 ns on PHP 8.4 and from 121-122 -> 53-54 ns on PHP 8.5, andjoinof 20 items from 316-319 -> 236-262 ns (433-440 -> 364-367 ns with anandseparator). A 300-row page using the four goes from 0.864-0.894 -> 0.749-0.785 ms. New fixture cases (numeric strings, empty arrays) pass on 3.x too.first/laston a 20-item array, time per call (best of 7 passes of 2,000,000 calls):3.xarray_first()everywhere, withsymfony/polyfill-php85\PHP_VERSION_ID >= 80500 ? array_first($item) : $item[array_key_first($item)](this PR)The polyfill costs one more userland call before PHP 8.5, so this PR does not require it. The version check is free at runtime: the opcodes of
first()on PHP 8.4 only contain thearray_key_first()call.joinon a 20-item array, time per call on PHP 8.4 (measured in a separate, slower session):join(', ')join(', ', ' and ')3.x